package com.nbcio.modules.crm.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.service.domain.CrmService;
import com.nbcio.modules.crm.service.service.ICrmServiceService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM客户服务管理控制器
 */
@Tag(name = "CRM客户服务管理")
@RestController
@RequestMapping("/crm/service")
@RequiredArgsConstructor
public class CrmServiceController {

    private final ICrmServiceService serviceService;

    /**
     * 分页查询客户服务
     */
    @GetMapping("/page")
    public R<IPage<CrmService>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String serviceNo,
            @RequestParam(required = false) String serviceName,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long contractId,
            @RequestParam(required = false) String serviceType,
            @RequestParam(required = false) String priority,
            @RequestParam(required = false) Long ownerUserId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        LambdaQueryWrapper<CrmService> qw = new LambdaQueryWrapper<>();
        if (serviceNo != null && !serviceNo.isEmpty()) {
            qw.like(CrmService::getServiceNo, serviceNo);
        }
        if (serviceName != null && !serviceName.isEmpty()) {
            qw.like(CrmService::getServiceName, serviceName);
        }
        if (customerId != null) {
            qw.eq(CrmService::getCustomerId, customerId);
        }
        if (contractId != null) {
            qw.eq(CrmService::getContractId, contractId);
        }
        if (serviceType != null && !serviceType.isEmpty()) {
            qw.eq(CrmService::getServiceType, serviceType);
        }
        if (priority != null && !priority.isEmpty()) {
            qw.eq(CrmService::getPriority, priority);
        }
        if (ownerUserId != null) {
            qw.eq(CrmService::getOwnerUserId, ownerUserId);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmService::getStatus, status);
        }
        if (startTime != null && endTime != null && !startTime.isEmpty() && !endTime.isEmpty()) {
            qw.between(CrmService::getRequestDate, startTime, endTime);
        }
        qw.orderByDesc(CrmService::getRequestDate);
        IPage<CrmService> page = serviceService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 根据ID查询客户服务
     */
    @GetMapping("/{id}")
    public R<CrmService> get(@PathVariable("id") String id) {
        CrmService service = serviceService.getById(Long.parseLong(id));
        return R.ok(service);
    }

    /**
     * 新增客户服务
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmService service) {
        return R.ok(serviceService.save(service));
    }

    /**
     * 更新客户服务
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmService service) {
        service.setServiceId(Long.parseLong(id));
        return R.ok(serviceService.updateById(service));
    }

    /**
     * 删除客户服务
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(serviceService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除客户服务
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(serviceService.removeByIds(longIds));
    }
}


